<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="../js/tools.js"></script>
    <script>

        const p1 = new Promise(resolve => {
            ajax({
                type: 'get',
                path: '../data/1.php',
                success: res => {
                    // console.log(res);
                    resolve(res);

                }
            })
        })

        const p2 = new Promise(resolve => {
            ajax({
                type: 'get',
                path: '../data/2.php',
                success: res => {
                    // console.log(res);
                    resolve(res);

                }
            })
        })


        const p3 = new Promise(resolve => {
            ajax({
                type: 'get',
                path: '../data/3.php',
                success: res => {
                    // console.log(res);
                    resolve(res);

                }
            })
        })


        // race 比赛    谁先取得结果，就把谁的结果作为新的Promise实例的结果
        const p = Promise.race([p1,p2,p3]) ;
        p.then(res => {
            console.log(res);
        })










        
        const pp1 = new Promise((resolve , reject) => {
            setTimeout(() => {
                const n = parseInt(Math.random() * 100) ;
                if(n % 2 === 0) {
                    resolve(n + '第一次成功') ;
                } else {
                    reject(n + '第一次失败') ;
                }
            },100)
        })

        const pp2 = new Promise((resolve , reject) => {
            setTimeout(() => {
                const n = parseInt(Math.random() * 100) ;
                if(n % 2 === 0) {
                    resolve(n + '第二次成功') ;
                } else {
                    reject(n + '第二次失败') ;
                }
            })
        })

        const pp3 = new Promise((resolve , reject) => {
            setTimeout(() => {
                const n = parseInt(Math.random() * 100) ;
                if(n % 2 === 0) {
                    resolve(n + '第三次成功') ;
                } else {
                    reject(n + '第三次失败') ;
                }
            })
        })

        const pp = Promise.race([pp1,pp2,pp3]) ;
        pp.then(res => {
            console.log(res);
        }).catch(err => {
            console.log(err);
        })

    </script>

</body>

</html>